package org.cxa.cableModemMonitor;
import org.jsoup.nodes.Element;

/**
 * Copyright 2014 opencxa.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Christopher Armenio
 *
 */
public class UpstreamChannelReading extends ChannelReading
{
	private final String channelType;
	private final long symbolRate;
	
	
	public UpstreamChannelReading(int expectedChannelNumIn, Element channelElementIn)
	{
		super(expectedChannelNumIn, channelElementIn.child(0), channelElementIn.child(1), channelElementIn.child(3), channelElementIn.child(5), channelElementIn.child(6));
		
		if( channelElementIn.children().size() != 7 ) throw new IllegalArgumentException(
				String.format("invalid number of children - got %d, expected %d", channelElementIn.children().size(), 7));
		
		this.channelType = channelElementIn.child(2).text();
		
		String symbolRateStrings[] = channelElementIn.child(4).text().split(" ");
		if( (symbolRateStrings.length != 2) || !symbolRateStrings[1].equals("Ksym/sec") ) throw new IllegalArgumentException(
				String.format("invalid symbol rate string - %s", channelElementIn.child(4).text()));
		this.symbolRate = Long.parseLong(symbolRateStrings[0]);
	}
	
	
	@Override
	public String getCsvString()
	{
		return String.format("%d, %s, %s, %d, %d, %f, %f", this.chanNum, this.chanStatus.toString(), this.channelType, this.chanId, this.symbolRate, this.frequency, this.power);
	}
	
	
	@Override
	public String toString()
	{
		return String.format("%d %s %s %d %d %f %f", this.chanNum, this.chanStatus.toString(), this.channelType, this.chanId, this.symbolRate, this.frequency, this.power);
	}
	
	
	public static String getCsvHeaderString()
	{
		return "Upstream ChanNum, ChanStatus, ChanType, ChanId, SymbolRate (Ksym/sec), Frequency (Hz), Power (dBmV)";
	}
}
